﻿using System;
using System.Text;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using HYView;

namespace YJKArch.DictModule
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdDict : YJKCommandManual
	{
		protected override Result YJKExcute(ExternalCommandData cmdData, ref string message, ElementSet elements)
		{
			CmdViewType types = (CmdViewType)1775;
			if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			Autodesk.Revit.DB.View activeView = cmdData.Application.ActiveUIDocument.ActiveView;
			XYZ baseVec = XYZ.BasisX;
			XYZ upVec = XYZ.BasisY;
			this.ViewValidating(cmdData.Application.ActiveUIDocument.Document, activeView, ref message);
			if (activeView.SketchPlane != null)
			{
				Plane plane = activeView.SketchPlane.GetPlane();
				baseVec = plane.XVec;
				upVec = plane.YVec;
			}
			for (;;)
			{
				using (DlgDict instance = DlgDict.GetInstance(cmdData))
				{
					DialogResult dialogResult = instance.ShowDialog();
					if (dialogResult == DialogResult.Cancel)
					{
						return Autodesk.Revit.UI.Result.Succeeded;
					}
					if (dialogResult == DialogResult.OK)
					{
						try
						{
							for (;;)
							{
								string text = instance.richTextBox1.Text.Trim();
								XYZ xyz = cmdData.Application.ActiveUIDocument.Selection.PickPoint("选择插入点");
								Transaction transaction = new Transaction(this.m_doc, "NewText");
								transaction.Start();
								try
								{
									TextNote textNode = this.m_doc.GetTextNode(this.m_doc.ActiveView, xyz, baseVec, upVec, 1.0, instance.alignFlags, text);
									byte[] array = null;
									if (text != null && text.Length > 0)
									{
										array = Encoding.Default.GetBytes(CmdDict.ReadTextMaxLenth(instance.richTextBox1));
									}
									FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_doc);
									filteredElementCollector.OfClass(typeof(TextNoteType));
									foreach (Element element in filteredElementCollector.ToElements())
									{
										if (element.Name == instance.cbxStyle.SelectedItem.ToString())
										{
											textNode.ChangeTypeId(element.Id);
											break;
										}
									}
									transaction.Commit();
									Transaction transaction2 = new Transaction(this.m_doc, "EditPosition");
									try
									{
										transaction2.Start();
										this.SetTextNoteWitdth(textNode, (double)array.Length);
										XYZ textNoteCoord = RevitElementExtension.GetTextNoteCoord(textNode);
										textNode.Location.Move(xyz - textNoteCoord);
										transaction2.Commit();
									}
									catch
									{
										transaction2.RollBack();
									}
								}
								catch
								{
									transaction.RollBack();
								}
							}
						}
						catch
						{
							continue;
						}
					}
					if (dialogResult == DialogResult.Yes)
					{
						try
						{
							TextFilter textFilter = new TextFilter();
							Reference reference = cmdData.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, textFilter, "拾取文字:");
							TextNote textNote = cmdData.Application.ActiveUIDocument.Document.GetElement(reference) as TextNote;
							for (;;)
							{
								XYZ xyz2 = cmdData.Application.ActiveUIDocument.Selection.PickPoint("选择插入点");
								Transaction transaction3 = new Transaction(this.m_doc, "Text");
								try
								{
									transaction3.Start();
                                    TextAlignFlags textAlign = (TextAlignFlags)128;
									string text2 = instance.cbxHorizonAlign.Text;
									if (!(text2 == "左对齐"))
									{
										if (!(text2 == "居中对齐"))
										{
											if (text2 == "右对齐")
											{
                                                textAlign = (TextAlignFlags)256;
											}
										}
										else
										{
                                            textAlign = (TextAlignFlags)128;
										}
									}
									else
									{
                                        textAlign = (TextAlignFlags)64;
									}
									TextNote textNode2 = this.m_doc.GetTextNode(this.m_doc.ActiveView, xyz2, baseVec, upVec, 1.0, textAlign, textNote.Text);
									FilteredElementCollector filteredElementCollector2 = new FilteredElementCollector(this.m_doc);
									filteredElementCollector2.OfClass(typeof(TextNoteType));
									foreach (Element element2 in filteredElementCollector2.ToElements())
									{
										if (element2.Name == instance.cbxStyle.SelectedItem.ToString())
										{
											textNode2.ChangeTypeId(element2.Id);
											break;
										}
									}
									transaction3.Commit();
									Transaction transaction4 = new Transaction(this.m_doc, "EditPosition");
									try
									{
										transaction4.Start();
										double textNoteWidth = RevitElementExtension.GetTextNoteWidth(textNote);
										this.SetTextNoteWitdth(textNode2, textNoteWidth);
										XYZ textNoteCoord2 = RevitElementExtension.GetTextNoteCoord(textNode2);
										textNode2.Location.Move(xyz2 - textNoteCoord2);
										transaction4.Commit();
									}
									catch
									{
										transaction4.RollBack();
									}
								}
								catch
								{
									transaction3.RollBack();
								}
							}
						}
						catch
						{
							continue;
						}
					}
					if (dialogResult == DialogResult.Abort)
					{
						Transaction transaction5 = new Transaction(this.m_doc, "Text");
						try
						{
							transaction5.Start();
							TextFilter textFilter2 = new TextFilter();
							Reference reference2 = cmdData.Application.ActiveUIDocument.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, textFilter2, "拾取要替换的文字:");
							TextNote textNote2 = cmdData.Application.ActiveUIDocument.Document.GetElement(reference2) as TextNote;
							FilteredElementCollector filteredElementCollector3 = new FilteredElementCollector(cmdData.Application.ActiveUIDocument.Document);
							filteredElementCollector3.OfClass(typeof(TextNote));
							if (filteredElementCollector3 != null)
							{
								foreach (Element element3 in filteredElementCollector3.ToElements())
								{
									if (((TextNote)element3).Text.Trim() == textNote2.Text.Trim())
									{
										((TextNote)element3).Text = instance.richTextBox1.Text;
									}
								}
							}
							transaction5.Commit();
						}
						catch
						{
							transaction5.RollBack();
						}
					}
				}
				break;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static string ReadTextMaxLenth(RichTextBox tb)
		{
			string result = null;
			int num = 0;
			string[] lines = tb.Lines;
			for (int i = 0; i < lines.Length; i++)
			{
				int num2 = Encoding.Default.GetBytes(lines[i]).Length;
				if (num2 > num)
				{
					num = num2;
					result = lines[i].ToString();
				}
			}
			return result;
		}

		private void ViewValidating(Autodesk.Revit.DB.Document doc, Autodesk.Revit.DB.View activeView, ref string message)
		{
			if (activeView.GetType() == typeof(ViewPlan))
			{
				ViewPlan viewPlan = activeView as Autodesk.Revit.DB.ViewPlan;
				if (viewPlan.SketchPlane == null)
				{
					Plane plane = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewPlan.Origin, viewPlan.RightDirection, viewPlan.UpDirection);
					Transaction transaction = new Transaction(doc);
					transaction.Start("new sketchPlane");
					SketchPlane sketchPlane = RevitVersionFuncs.NewSketchPlane(doc, plane);
					activeView.SketchPlane = sketchPlane;
					transaction.Commit();
					return;
				}
			}
			else if (activeView.GetType() == typeof(ViewSection))
			{
				ViewSection viewSection = activeView as Autodesk.Revit.DB.ViewSection;
				if (viewSection.SketchPlane == null)
				{
					Plane plane2 = RevitVersionFuncs.CreatePlanByOriginAndBasis(viewSection.Origin, viewSection.RightDirection, viewSection.UpDirection);
					Transaction transaction2 = new Transaction(doc);
					transaction2.Start("new sketchPlane");
					SketchPlane sketchPlane2 = RevitVersionFuncs.NewSketchPlane(doc, plane2);
					activeView.SketchPlane = sketchPlane2;
					transaction2.Commit();
					return;
				}
			}
			else if (activeView.GetType() == typeof(ViewSheet) && (activeView as Autodesk.Revit.DB.ViewSheet).SketchPlane == null)
			{
				Transaction transaction3 = new Transaction(doc);
				transaction3.Start("new sketchPlane");
				Line line = Line.CreateBound(new XYZ(0.0, 0.0, 0.0), new XYZ(0.1, 0.0, 0.0));
				DetailCurve detailCurve = doc.Create.NewDetailCurve(activeView, line);
				doc.Regenerate();
				doc.Delete(detailCurve.Id);
				transaction3.Commit();
			}
		}

		public void SetTextNoteWitdth(TextNote textNote, double width)
		{
			RevitElementExtension.SetTextNoteWidth(textNote, width);
		}
	}
}
